package org.refresh.core.intelligence;

import java.util.ArrayList;
import java.util.List;

/*******************************************************************************
 * Copyright (c) 2005 Jules White. All rights reserved. This program and the
 * accompanying materials are made available under the terms of the Common
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
public class IteratorQueryEvaluator implements QueryEvaluator {

	private ConstraintEvaluatorProvider constraintEvaluatorProvider_;

	public IteratorQueryEvaluator(
			ConstraintEvaluatorProvider constraintEvaluatorProvider) {
		super();
		constraintEvaluatorProvider_ = constraintEvaluatorProvider;
	}

	public List findAll(Object src, List world, List<Constraint> cons) {
		ArrayList result = new ArrayList();
		for (Constraint con : cons) {
			ConstraintEvaluator eval = constraintEvaluatorProvider_
					.getConstraintEvaluator(con.getType());
			result.clear();
			for (Object o : world) {
				if(eval.evaluate(src, o, con)){
					result.add(o);
				}
			}
			world = result;
		}
		return result;
	}

}
